<?php
/**
 * MAC地址版本的设备指令API测试
 */

// API配置
$apiKey = 'ak_3d97ebf664db97f3023b336f5fc0ad23';
$secretKey = 'e61b4d35cf2a29e46e08e36dd075ea585614cd91470719c4411d2b946f677e12';
$baseUrl = 'http://localhost:8000';

/**
 * 生成API签名
 */
function generateSignature($method, $path, $body, $secretKey)
{
    $timestamp = time();
    $stringToSign = strtoupper($method) . "\n" . $path . "\n" . $timestamp . "\n" . $body;
    $signature = hash_hmac('sha256', $stringToSign, $secretKey);
    
    return [
        'timestamp' => $timestamp,
        'signature' => $signature
    ];
}

/**
 * 发送设备指令
 */
function sendDeviceCommand($mac, $type, $apiKey, $secretKey, $baseUrl)
{
    // 准备请求数据
    $url = $baseUrl . '/api/device/command';
    $method = 'POST';
    $path = 'api/device/command';
    $data = [
        'mac' => $mac,
        'type' => $type
    ];
    $body = json_encode($data, JSON_UNESCAPED_UNICODE);
    
    // 生成签名
    $auth = generateSignature($method, $path, $body, $secretKey);
    
    // 构建请求头
    $headers = [
        'Authorization: ApiKey ' . $apiKey,
        'Timestamp: ' . $auth['timestamp'],
        'Signature: ' . $auth['signature'],
        'Content-Type: application/json',
        'X-Request-ID: req_' . uniqid()
    ];
    
    echo "发送设备指令:\n";
    echo "MAC地址: {$mac}\n";
    echo "指令类型: {$type}\n";
    echo "请求URL: {$url}\n";
    echo "请求体: {$body}\n";
    echo "时间戳: {$auth['timestamp']}\n";
    echo "签名: {$auth['signature']}\n\n";
    
    // 初始化cURL
    $ch = curl_init();
    
    // 设置cURL选项
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
    // 执行请求
    $startTime = microtime(true);
    $response = curl_exec($ch);
    $endTime = microtime(true);
    
    // 获取响应信息
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    $error = curl_error($ch);
    
    curl_close($ch);
    
    // 输出结果
    echo "响应结果:\n";
    echo "HTTP状态码: {$httpCode}\n";
    echo "Content-Type: {$contentType}\n";
    echo "响应时间: " . round(($endTime - $startTime) * 1000, 2) . "ms\n";
    
    if ($error) {
        echo "cURL错误: {$error}\n";
        return false;
    }
    
    echo "响应内容:\n";
    if ($response) {
        $jsonData = json_decode($response, true);
        if (json_last_error() === JSON_ERROR_NONE) {
            echo json_encode($jsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
            return $jsonData;
        } else {
            echo $response . "\n";
            return $response;
        }
    } else {
        echo "无响应内容\n";
        return false;
    }
}

echo "=== MAC地址版本设备指令API测试 ===\n\n";

// 测试1: 设备重启
echo "1. 设备重启测试\n";
echo str_repeat("-", 40) . "\n";
$result = sendDeviceCommand('1C3426522542', 'reboot', $apiKey, $secretKey, $baseUrl);
echo "\n";

// // 测试2: 热点配置
// echo "2. 热点配置测试\n";
// echo str_repeat("-", 40) . "\n";
// $result = sendDeviceCommand('1C3426522542', 'hotspot_config', $apiKey, $secretKey, $baseUrl);
// echo "\n";

// // 测试3: SIM卡切换
// echo "3. SIM卡切换测试\n";
// echo str_repeat("-", 40) . "\n";
// $result = sendDeviceCommand('1C3426522542', 'sim_switch_slot1', $apiKey, $secretKey, $baseUrl);
// echo "\n";

// // 测试4: 网络限速
// echo "4. 网络限速测试\n";
// echo str_repeat("-", 40) . "\n";
// $result = sendDeviceCommand('1C3426522542', 'speed_limit_10m', $apiKey, $secretKey, $baseUrl);
// echo "\n";

// // 测试5: 立即上报
// echo "5. 立即上报测试\n";
// echo str_repeat("-", 40) . "\n";
// $result = sendDeviceCommand('1C3426522542', 'report_status', $apiKey, $secretKey, $baseUrl);
// echo "\n";

// echo "=== 测试完成 ===\n";
// echo "注意：现在使用MAC地址 '1C3426522542' 作为设备标识\n";
// echo "MQTT主题格式：tc/device/in/1C3426522542\n";
?>
